﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ClassLibrary;

namespace Program
{
    public partial class UploadNewContent : System.Web.UI.Page
    {
        ContentCollection content = new ContentCollection();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnUpload_Click(object sender, EventArgs e) 
        {
            if (IsPostBack)
            {
                Boolean fileOK = false;
                String path = Server.MapPath("~/upload/");
                if (FileUpload.HasFile)
                {
                    String fileExtension =
                        System.IO.Path.GetExtension(FileUpload.FileName).ToLower();
                    String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" };
                    for (int i = 0; i < allowedExtensions.Length; i++)
                    {
                        if (fileExtension == allowedExtensions[i])
                        {

                            fileOK = true;

                        }
                    }
                }

                if (fileOK)
                {
                    string rename = TbxFilename.Text + System.IO.Path.GetExtension(FileUpload.FileName).ToLower();
                    try
                    {
                        if (TbxFilename.Text != "")
                        {
                            FileUpload.PostedFile.SaveAs(path
                                + rename);
                            Label1.Text = "File uploaded!";
                        }
                        else
                        {
                            FileUpload.PostedFile.SaveAs(path
                                + FileUpload.FileName);
                            Label1.Text = "File uploaded!";
                        }
                        TbxFilename.Text = "";
                        content.CreateContent(TbxOverskrift.Text, TbxDescription.Text, FileUpload.FileName.ToString());

                    }
                    catch (Exception)
                    {
                        Label1.Text = "File could not be uploaded.";
                    }
                }
                else if (TbxLink.Text != "")
                {
                    content.CreateContent(TbxOverskrift.Text, TbxDescription.Text, TbxLink.Text);
                }
                else
                {
                    Label1.Text = "Kan ikke uploade denne filtype, eller der er ikke sat noget link ind";
                }
            }
        }
    }
}